package net.lqy.aipan.controller;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import net.lqy.aipan.config.MinioConfig;
import net.lqy.aipan.util.CommonUtil;
import net.lqy.aipan.util.JsonData;
import org.simpleframework.xml.core.Resolve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.security.PrivateKey;

/**
 * @title: TestController
 * @Author lqy
 * @Date: 2025-05-30 23:15
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1/test")
public class TestController {
    @Autowired
    private MinioConfig minioConfig;
//    @Autowired
//    private MinioClient minioClient;

//    测试文件上传
    @PostMapping("/upload")
    public JsonData testUpload(@RequestParam("file") MultipartFile file) {
        String filename = CommonUtil.getFilePath(file.getOriginalFilename());

        //读取文件流，上传到minio
        try{
            InputStream inputStream = file.getInputStream();

            //minio原生操作
//            minioClient.putObject(PutObjectArgs.builder()
//                    .bucket(minioConfig.getBucketName())
//                    .object(filename).stream(inputStream,inputStream.available(),-1)
//                    .build());


        }catch (Exception e){
            e.printStackTrace();
            return JsonData.buildError("上传失败");
        }

        String url=minioConfig.getEndpoint()+"/"+minioConfig.getBucketName()+"/"+filename;
        return JsonData.buildSuccess(url);
    }
}
